<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增用户</title>
    <link rel="stylesheet" href="/element-ui/index.css">
    <link rel="stylesheet" href="/element-ui/fonts/element-icons.css">
    <script src="/element-ui/vue.js"></script>
    <script src="/element-ui/index.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', '\\5FAE\\8F6F\\96C5\\9ED1', Arial, sans-serif;
            background-color: #f5f6fa;
        }
        #app {
            padding: 20px;
            background-color: #f5f6fa;
        }
        .page-title-bar {
            font-size: 26px;
            font-weight: bold;
            color: #333;
            background: #fff;
            padding: 24px 20px 12px 20px;
            border-bottom: 1px solid #eee;
            margin-bottom: 20px;
            letter-spacing: 2px;
            text-align: center;
            animation: fadeInDown 0.8s ease-out;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
            border-radius: 8px;
        }
        @keyframes fadeInDown {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        .el-main {
            padding: 20px;
            background: #fff;
            border-radius: 8px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        }
        .el-form-item {
            margin-bottom: 22px;
        }
        .el-input__inner,
        .el-select .el-input__inner {
            transition: all 0.3s ease;
        }
        .el-input__inner:focus,
        .el-input__inner:hover,
        .el-select .el-input__inner:focus,
        .el-select .el-input__inner:hover {
            border-color: #409EFF;
            box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
        }
        .el-button {
            transition: all 0.3s ease;
        }
        .el-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
        }
        .el-button--primary:hover {
            box-shadow: 0 4px 10px rgba(64, 158, 255, 0.2);
        }
    </style>
</head>
<body>
    <div class="page-title-bar">新增用户</div>
    <div id="app">
        <el-container>
            <el-main>
                <el-form :model="form" :rules="rules" ref="form" label-width="100px">
                    <el-form-item label="用户类型" prop="status">
                        <el-radio-group v-model="form.status">
                            <el-radio :label="0">普通用户</el-radio>
                            <el-radio :label="1">管理员</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="form.username"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="form.password" type="password" autocomplete="new-password"></el-input>
                    </el-form-item>
                    <el-form-item label="真实姓名" prop="realName">
                        <el-input v-model="form.realName"></el-input>
                    </el-form-item>
                    <el-form-item label="院系" prop="department" v-if="form.status === 0">
                        <el-select v-model="form.department" placeholder="请选择院系">
                            <el-option
                                v-for="item in departments"
                                :key="item"
                                :label="item"
                                :value="item">
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="院系" v-else>
                        <el-input v-model="form.department" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="电话" prop="phone">
                        <el-input v-model="form.phone"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="submitForm('form')">提交</el-button>
                        <el-button @click="goBack">返回</el-button>
                    </el-form-item>
                </el-form>
            </el-main>
        </el-container>
    </div>
    <script>
        new Vue({
            el: '#app',
            data() {
                return {
                    form: {
                        status: 0,
                        username: '',
                        password: '',
                        realName: '',
                        department: '',
                        phone: ''
                    },
                    rules: {
                        status: [
                            { required: true, message: '请选择用户类型', trigger: 'change' }
                        ],
                        username: [
                            { required: true, message: '请输入用户名', trigger: 'blur' }
                        ],
                        password: [
                            { required: true, message: '请输入密码', trigger: 'blur' }
                        ],
                        realName: [
                            { required: true, message: '请输入真实姓名', trigger: 'blur' }
                        ],
                        department: [
                            { required: true, message: '请输入院系', trigger: 'blur' }
                        ],
                        phone: [
                            { required: true, message: '请输入电话', trigger: 'blur' }
                        ]
                    },
                    departments: [
                        '计算机系', '金融系', '电子商务系', '大数据系', '人工智能系',
                        '艺术设计系', '医学系', '环境设计系', '音乐系', '老年管理系'
                    ]
                }
            },
            methods: {
                watch: {
                    'form.status'(val) {
                        if (val === 1) {
                            this.form.department = '管理部门';
                        } else {
                            this.form.department = '';
                        }
                    }
                },
                submitForm(formName) {
                    this.$refs[formName].validate((valid) => {
                        if (valid) {
                            axios.post('/user/add', this.form)
                                .then(response => {
                                    if (response.data === 'success') {
                                        this.$message.success('添加成功');
                                        this.goBack();
                                    } else if (response.data === 'username_exist') {
                                        this.$message.error('用户名已存在');
                                    } else {
                                        this.$message.error('添加失败');
                                    }
                                })
                                .catch(error => {
                                    this.$message.error('添加失败');
                                });
                        } else {
                            return false;
                        }
                    });
                },
                goBack() {
                    window.location.href = '/user/list';
                }
            }
        });
    </script>
</body>
</html> 